package com.wang.users.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IAAdminsDao;
import com.wang.users.dao.IAUsersDao;
import com.wang.users.pojo.AAdmins;
import com.wang.users.pojo.AUsers;
import com.wang.users.service.IUsersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 一个模块一个Service
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
    @Resource
    private IAAdminsDao adminsDao;
    @Resource
    private IAUsersDao usersDao ;

    /**
     * {
     *      "code":"响应码",
     *      "info":"响应信息",
     *      "data":
     *       {
     *      	"id:":"",
     *      	"effect":""
     *       }
     *  }
     * @param admins   朝代
     * @return
     */
    @Override
    public JSONObject saveOneAdminsService(AAdmins admins)
    {
        JSONObject resultJSON = new JSONObject();
        admins.setCreateTime(new Date());
        admins.setUpdateTime(new Date());

        int res = this.adminsDao.saveOne(admins);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",admins.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject updateOneAdminsService(AAdmins admins)
    {
        JSONObject resultJSON = new JSONObject();

        admins.setUpdateTime(new Date());
        int res = this.adminsDao.updateOne(admins);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",admins.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject deleteBatchAdminsService(Map<String, Object> condMap)
    {
        JSONObject resultJSON = new JSONObject();
        int res = this.adminsDao.deleteBatch(condMap);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public AAdmins selectOneAdminsService(Map<String, Object> condMap)
    {
        return this.adminsDao.selectOne(condMap);
    }

    @Override
    public List<AAdmins> selectCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
    {
        /* 查询之前,先解决keyword加%的问题 */
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
        {
            condMap.put("keyword", "%" + condMap.get("keyword") + "%");
        }

        if (pageInfoUtil != null)
        {
            Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            List<AAdmins> list = this.adminsDao.selectCondList(condMap);
            /* 设置总条数 */
            pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
            /* 返回结果 */
            return list ;
        }
        return this.adminsDao.selectCondList(condMap);
    }

    /**
     * {
     *      "code":"响应码",
     *      "info":"响应信息",
     *      "data":
     *       {
     *      	"id:":"",
     *      	"effect":""
     *       }
     *  }
     * @param users   朝代
     * @return
     */
    @Override
    public JSONObject saveOneUsersService(AUsers users)
    {
        JSONObject resultJSON = new JSONObject();
        users.setCreateTime(new Date());
        users.setUpdateTime(new Date());
        users.setLastLoginTime(new Date());

        /**
         * 用户名不能重复
         */
        Map<String,Object> condMap = new HashMap<>();
        condMap.put("email",users.getEmail());
        AUsers usersRes = this.selectOneUsersService(condMap);
        if(usersRes != null)
        {
            /* 说明查询到了,不能重复 */
            resultJSON.put("code","1");
            resultJSON.put("info","邮箱已存在");
            return resultJSON ;
        }

        int res = this.usersDao.saveOne(users);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",users.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject updateOneUsersService(AUsers users)
    {
        JSONObject resultJSON = new JSONObject();

        users.setUpdateTime(new Date());
        int res = this.usersDao.updateOne(users);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",users.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject deleteBatchUsersService(Map<String, Object> condMap)
    {
        JSONObject resultJSON = new JSONObject();
        int res = this.usersDao.deleteBatch(condMap);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public AUsers selectOneUsersService(Map<String, Object> condMap)
    {
        return this.usersDao.selectOne(condMap);
    }

    @Override
    public List<AUsers> selectCondListUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
    {
        /* 查询之前,先解决keyword加%的问题 */
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
        {
            condMap.put("keyword", "%" + condMap.get("keyword") + "%");
        }

        if (pageInfoUtil != null)
        {
            Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            List<AUsers> list = this.usersDao.selectCondList(condMap);
            /* 设置总条数 */
            pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
            /* 返回结果 */
            return list ;
        }
        return this.usersDao.selectCondList(condMap);
    }
}
